#   Copyright 2006 Jerome Lacoste (jerome@coffeebreaks.org)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
require 'date'

class TimePeriod
  # FIXME finds one somewhere or add inclusivity

  attr_reader :from
  attr_reader :to

  def initialize(from, to)
    @from = from
    @to = to
    unless from.nil? or to.nil? or from <= to
      raise ArgumentError, 'Invalid period ' + to_s
    end
  end

#  def <=
#  end

  def to_s
    "TimePeriod: #@from - #@to"
  end

  def self.for_day(date = Time.now)
     from = date.at_beginning_of_day
     to = from + 24.hours
     TimePeriod.new(from, to)
  end

  # return the date as a day (removes hour and below)
  def self.as_day(date = Time.now)
    Time.local(date.year, date.month, date.day)
  end


  def self.this_month(date = Time.now)
    from = date.at_beginning_of_month
    #to = date.next_month
    to = date.at_end_of_month + 1.day - 1.minute
    TimePeriod.new(from, to)
  end
end
